package com.gaogzhen.threadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Slf4j(topic = "TestThreadPollExecutor03")
public class TestThreadPollExecutor04 {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(2);

        test03(pool);

    }

    public static void test03(ExecutorService pool) {
        Future<Integer> f1 = pool.submit(() -> {
            log.debug("task1 is running ....");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task1 finished ...");
            return 1;
        });

        Future<Integer> f2 = pool.submit(() -> {
            log.debug("task2 is running ....");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task2 finished ...");
            return 2;
        });

        Future<Integer> f3 = pool.submit(() -> {
            log.debug("task3 is running ....");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task3 finished ...");
            return 3;
        });

        pool.shutdown();

        log.debug("others...");
    }

    public static void test02(ExecutorService pool) {
        Future<Integer> f1 = pool.submit(() -> {
            log.debug("task1 is running ....");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task1 finished ...");
            return 1;
        });

        Future<Integer> f2 = pool.submit(() -> {
            log.debug("task2 is running ....");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task2 finished ...");
            return 2;
        });

        Future<Integer> f3 = pool.submit(() -> {
            log.debug("task3 is running ....");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task3 finished ...");
            return 3;
        });

        pool.shutdown();

        Future<Integer> f4 = pool.submit(() -> {
            log.debug("task4 is running ....");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task4 finished ...");
            return 4;
        });
    }

    public static void test01(ExecutorService pool) {
        Future<Integer> f1 = pool.submit(() -> {
            log.debug("task1 is running ....");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task1 finished ...");
            return 1;
        });

        Future<Integer> f2 = pool.submit(() -> {
            log.debug("task2 is running ....");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task2 finished ...");
            return 2;
        });

        Future<Integer> f3 = pool.submit(() -> {
            log.debug("task3 is running ....");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task3 finished ...");
            return 3;
        });

        pool.shutdown();
    }
}
